﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using REPORT.DTO;
using REPORT.DAL;
namespace REPORT.DAL
{
    public class DAL_SANPHAM
    {
        public DataTable LoadLoaiSP()
        {
            DbProvider db = new DbProvider();
            DataTable dt = new DataTable();
            string sql = @"select * from loaisanpham";
            try
            {
                dt = db.ExecuteQuery(sql);
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return dt;
        }
        public DataTable LoadSPTheoLoai(int maloaisp)
        {
            DbProvider db = new DbProvider();
            DataTable dt = new DataTable();
            string sql = @"select * from sanpham where ma_loai_san_pham=@maloaisp and so_luong > 0";
            db.AddParameter(new SqlParameter("@maloaisp", maloaisp));
            try
            {
                dt = db.ExecuteQuery(sql);
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return dt;
        }
        public int UpdateSoLuong(DTO_SANPHAM obj)
        {
            DbProvider db = new DbProvider();
            int result = 0;
            string strInsert = @"UPDATE sanpham SET so_luong=so_luong-@sl WHERE ma_san_pham=@masp";
            db.AddParameter(new SqlParameter("@sl", obj.SoLuong));
            db.AddParameter(new SqlParameter("@masp", obj.MaSanPham));
            try
            {
                result = db.ExcuteNoneQuery(strInsert);
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return result;
        }
        public int UpdateSoLuongKhiXoaKhoiCTHD(DTO_SANPHAM obj)
        {
            DbProvider db = new DbProvider();
            int result = 0;
            string strInsert = @"UPDATE sanpham SET so_luong=so_luong+@sl WHERE ma_san_pham=@masp";
            db.AddParameter(new SqlParameter("@sl", obj.SoLuong));
            db.AddParameter(new SqlParameter("@masp", obj.MaSanPham));
            try
            {
                result = db.ExcuteNoneQuery(strInsert);
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return result;
        }
    }
}
